package com.restphp.java.utils.example.mysql.builder;

import com.alibaba.fastjson.JSONObject;
import com.restphp.java.utils.mysql.builder.core.SqlBuilder;
import com.restphp.java.utils.mysql.builder.enums.ExpressEnum;
import com.restphp.java.utils.mysql.builder.enums.OrderEnum;
import com.restphp.java.utils.mysql.builder.po.NativePo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sofical on 2020/7/3.
 */
public class TestWithoutDb {
    public static void main(String[] args) {
        //查询语句
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.setTable("user");
        sqlBuilder.getCondition().add("userName", "sofical").add("age", ExpressEnum.GT, 18);
        sqlBuilder.setOrder("userId", OrderEnum.DESC).setLimit(10, 1);
        NativePo select = sqlBuilder.getSelect();
        System.out.print("SELECT SQL: ");
        System.out.println(select.getSql());
        System.out.print("parameter: ");
        System.out.println(JSONObject.toJSONString(select.getValues()));

        //插入语句
        SqlBuilder insertBuilder = new SqlBuilder();
        insertBuilder.setTable("user");
        insertBuilder.getValues().set("userName", "sofical").set("age", 19);
        NativePo insert = insertBuilder.getInsert();
        System.out.print("INSERT SQL: ");
        System.out.println(insert.getSql());
        System.out.print("parameter: ");
        System.out.println(JSONObject.toJSONString(insert.getValues()));

        //更新语句
        SqlBuilder updateBuilder = new SqlBuilder();
        updateBuilder.setTable("user");
        updateBuilder.getValues().set("age", 19).set("sex", "male");
        updateBuilder.getCondition().add("userId", 1);
        NativePo update = updateBuilder.getUpdate();
        System.out.print("UPDATE SQL: ");
        System.out.println(update.getSql());
        System.out.print("parameter: ");
        System.out.println(JSONObject.toJSONString(update.getValues()));

        //删除语句
        List<Integer> userIdList = new ArrayList<>();
        userIdList.add(10);
        userIdList.add(11);
        SqlBuilder deleteBuilder = new SqlBuilder();
        deleteBuilder.setTable("user");
        deleteBuilder.getCondition().add("userId", ExpressEnum.IN, userIdList);
        NativePo delete = deleteBuilder.getDelete();
        System.out.print("DELETE SQL: ");
        System.out.println(delete.getSql());
        System.out.print("parameter: ");
        System.out.println(JSONObject.toJSONString(delete.getValues()));
    }
}
